extends Node2D


# Declare member variables here. Examples:
onready var player:RigidBody2D = $Player;
onready var animationPlayer:AnimationPlayer = $Player/AnimationPlayer
onready var camera:Camera2D = $Camera2D
var currentAnimation = "start";
var speed = 200;



# Called when the node enters the scene tree for the first time.
func _ready():
	camera.position = player.position


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	var velocity = Vector2(0,0)
	var isMoving = false
	var newAnimation = currentAnimation
	
	if Input.is_action_pressed("left"):
		velocity.x += -1
		newAnimation = "left"
		isMoving = true
	if Input.is_action_pressed("right"):
		velocity.x += 1
		newAnimation = "right"
		isMoving = true
	if Input.is_action_pressed("up"):
		velocity.y += -1
		newAnimation = "up"
		isMoving = true
	if Input.is_action_pressed("down"):
		velocity.y += 1
		newAnimation = "down"
		isMoving = true
	
	player.linear_velocity = velocity
	player.angular_velocity = 0
	
	if velocity.length() > 0:
#		player.position += velocity.normalized() * speed * delta
		player.linear_velocity = velocity.normalized()*speed
		updateCamera()
	
	updateAnimation(isMoving,newAnimation)
	
func updateCamera():
	camera.position = player.position
	
func updateAnimation(isMoving,newAnimation):
	if isMoving and currentAnimation != newAnimation:
		animationPlayer.current_animation = newAnimation
		animationPlayer.play(newAnimation)
		currentAnimation = newAnimation
	elif !isMoving and currentAnimation != "start":
		animationPlayer.stop()
		currentAnimation = 'start'
	
